package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.ShopBaseController;
import com.yunc.base.utils.R;
import com.yunc.base.utils.SecurityUtils;
import com.yunc.modules.shop.entity.ShopItemCat;
import com.yunc.modules.shop.service.IShopItemCatService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 商品分类
 *
 * @author congcong tao
 * @date 2018/12/24 9:32
 */
@RestController
@RequestMapping("shopItemCat")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopItemCatController extends ShopBaseController {

    private final IShopItemCatService shopItemCatService;
    
    @GetMapping("/page")
    public R page(Page<ShopItemCat> page, ShopItemCat itemCat){
        QueryWrapper<ShopItemCat> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(itemCat.getTitle())) {
            qw.eq("title", itemCat.getTitle());
        }
        setShop(qw);
        shopItemCatService.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/listSelect")
    public R listSelect(){
        QueryWrapper<ShopItemCat> qw = new QueryWrapper<>();
        setShop(qw);
        qw.select("id", "title");
        List<ShopItemCat> list = shopItemCatService.list(qw);
        return R.ok().put("list", list);
    }

    @GetMapping("/list")
    public R list(ShopItemCat itemCat){
        QueryWrapper<ShopItemCat> qw = new QueryWrapper<>();
        setShop(qw);
        List<ShopItemCat> list = shopItemCatService.list(qw);
        return R.ok().put("list", list).put("itemCat", itemCat);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        ShopItemCat itemCat = shopItemCatService.getById(id);
        return R.ok().put("itemCat", itemCat);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(ShopItemCat itemCat){
        setShopId(itemCat);
        itemCat.setCreator(SecurityUtils.getUserId());
        itemCat.setCreateTime(new Date());
        boolean flag = shopItemCatService.save(itemCat);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(ShopItemCat itemCat){
        boolean flag = shopItemCatService.updateById(itemCat);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
